<template>
  <div class="app-container">
    <div class="head-container">
      <el-input v-model="params.crtUserName" clearable placeholder="操作用户" style="width: 150px;" class="filter-item" @keyup.enter.native="handleQuery"/>
      <el-input v-model="params.description" clearable placeholder="操作描述" style="width: 150px;" class="filter-item" @keyup.enter.native="handleQuery"/>
      <el-select v-model="params.logType" clearable placeholder="日志类型" style="width: 150px;" class="filter-item" @keyup.enter.native="handleQuery">
        <el-option v-for="item in logTypeOptions" :key="item.id" :value="item.value" :label="item.label"/>
      </el-select>
      <el-button size="mini" type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
      <el-button size="mini" type="button" icon="el-icon-refresh" @click="handleReset">重置</el-button>
    </div>
    <!--表格渲染-->
    <el-table v-loading="loading" :data="data" :height="tableHeight" size="mini" style="width: 100%;" fit border >
      <el-table-column prop="crtUserName" header-align="center" label="操作用户" align="center" width="90px"/>
      <el-table-column prop="requestIp" header-align="center" label="请求IP" align="center" width="80px"/>
      <el-table-column prop="description" header-align="center" label="操作描述" align="center" width="100px" show-overflow-tooltip/>
      <el-table-column show-overflow-tooltip header-align="center" prop="requestMethod" label="方法名称"/>
      <el-table-column show-overflow-tooltip header-align="center" prop="requestParam" label="传输参数"/>
      <el-table-column prop="logType" label="日志类型" align="center" width="80px">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.logType==='S'" size="mini" >成功</el-tag>
          <el-tag v-else type="danger" size="F" @click="info(scope.row.logId)">失败</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="responseTime" label="请求耗时" align="center" width="90px">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.responseTime <= 300" size="mini">{{ scope.row.responseTime }}ms</el-tag>
          <el-tag v-else-if="scope.row.responseTime <= 1000" size="mini" type="warning">{{ scope.row.responseTime }}ms</el-tag>
          <el-tag v-else type="danger" size="mini">{{ scope.row.responseTime }}ms</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="crtTime" align="center" label="请求日期" width="180px">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.crtTime) }}</span>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <el-pagination :total="total" style="margin-top: 8px;" layout="total, prev, pager, next, sizes,jumper"
      @size-change="sizeChange" @current-change="pageChange" :current-page.sync="page"/>
      <el-dialog :visible.sync="dialog" title="异常详情" append-to-body top="0" width="85%">
      <span>
        {{ errorInfo }}
      </span>
    </el-dialog>
  </div>
</template>

<script>
import initData from '@/mixins/initData'
import tableHeight from '@/mixins/tableHeight'
import { get } from '@/api/base/dictDetail'
import { getErrDetail } from '@/api/base/log'
import { parseTime } from '@/utils/index'
export default {
  mixins: [initData, tableHeight],
  data () {
    return {
      logTypeOptions: [],
      errorInfo: '',
      dialog: false
    }
  },
  created () {
    this.$nextTick(() => {
      this.init()
    })
    get('sys_log_type').then(response => {
      this.logTypeOptions = response.data
    })
  },
  methods: {
    parseTime,
    beforeInit () {
      this.url = 'api/monitor/log/page'
      return true
    },
    handleQuery () {
      this.page = 1
      this.init()
    },
    handleReset () {
      this.params.crtUserName = undefined
      this.params.description = undefined
      this.params.logType = undefined
      this.init()
    },
    info (id) {
      this.dialog = true
      getErrDetail(id).then(res => {
        this.errorInfo = res.exceptionDetail
      })
    }
  }
}
</script>

<style scoped>

</style>
